from PaSt.past.models import Pandoleros
from PaSt.past.models import Turns

class Team:
    t = []
            
            
    def __init__(self,p=None):
        
        if(p==None):
            self.t = [turn for turn in Turns.objects.all() if turn.idP >= 7 and turn.idP <=12]
            
        elif p == 'trening':
            self.t = [turn for turn in Turns.objects.all() if turn.idP >= 7 and turn.idP <=12 and turn.turn_type == 1]

        elif p == 'tekma':
            self.t = [turn for turn in Turns.objects.all() if turn.idP >= 7 and turn.idP <=12 and turn.turn_type == 0]

    #######
    #NAPAD#
    #######
    
    def scoreVector(self):
        s =  [turn.final_score for turn in self.t]
        return s
    
    def scoreAverage(self):
        v=self.scoreVector()
        return float(sum(v))/len(v)   
    
    def scorePositive(self):
        v=self.scoreVector()
        u=[score for score in v if score>0]
        return float(len(u))/len(v) 
    
    def scoreChance(self):
        u=[turn for turn in self.t if turn.hit1_id>-1024]
        return float(len(u))/len(self.t)
    
    
        
    
    #########
    #OBRAMBA#
    #########
    
    def caughtServCount(self):
        return len([turn for turn in self.t if turn.serv1_id>0 or turn.serv2_id>0 or turn.serv3_id>0])

    def caughtHitCount(self):
        return len([turn for turn in self.t if turn.hit1_id>0 or turn.hit2_id>0 or turn.hit3_id>0])
    
    def baseHitCount(self):
        return len([turn for turn in self.t if turn.base_def_id>0])

    def baseHitServRatio(self):
        sc = len([turn for turn in self.t if (turn.serv1_id==0 or turn.serv2_id==0 or turn.serv3_id==0)])
        hc = len([turn for turn in self.t if turn.base_def_id>0])
        return float(hc)/sc
    
    def baseDefServRatio(self):
        sc = len([turn for turn in self.t if turn.serv1_id==0 or turn.serv2_id==0 or turn.serv3_id==0])
        hc = len([turn for turn in self.t if turn.base_def_id==0])
        return float(hc)/sc
    
    
    ######
    #MISC#
    ######
    
    def scoreZeroReason(self):
        #neuspesna serva, izbita baza, ulovljen pandolo, neuspesen udarec
        vector=[0,0,0,0]
        vector[2] = self.caughtServCount() + self.caughtHitCount()
        
        v = [turn for turn in self.t if turn.final_score == 0]
        for turn in v:
            if  (turn.serv1_id<0) and (turn.serv2_id<0) and (turn.serv3_id<0):
                vector[0]+=1
            elif turn.base_def_id>0:
                vector[1]+=1
            elif (turn.hit1_id<0 and turn.hit1_id>-1024) and (turn.hit2_id<0 and turn.hit2_id>-1024) and (turn.hit3_id<0 and turn.hit3_id>-1024):
                vector[3]+=1
        vector[2] = self.caughtServCount() + self.caughtHitCount()
        
        return vector